package com.ciaojian.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.mapper.StorePerformanceMapper;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.vo.StoreCounponNumVo;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.model.query.DashboardQuery;
import com.ciaojian.store.service.StorePerformanceServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author Atlakyn
 */
@Api(tags = "业绩数据")
@RestController
@RequestMapping("/performance")
public class StorePerformanceController {
    @Resource
    private StorePerformanceServiceImpl storePerformanceService;
    @Resource
    private StorePerformanceMapper storePerformanceMapper;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 全类型(含所有项目、会员)
     */
    private final List<Integer> ALL_TYPE = new ArrayList<>(Arrays.asList(1, 9, 11));
    /**
     * 剪发订单
     */
    private final List<Integer> CUT_HAIR_TYPE = new ArrayList<>(Arrays.asList(1, 9));
    /**
     * 会员订单
     */
    private final List<Integer> MEMBER_DETAIL_TYPE = new ArrayList<>(Collections.singletonList(11));

    /**
     * 抖音订单
     */
    private final List<Integer> DOUYIN_TYPE = new ArrayList<>(Collections.singletonList(4));



    @ApiOperation("员工业绩")
    @GetMapping("/worker-rank")
    public Result workerPerformanceRank(@Validated DashboardQuery query) {
        return Result.ok(storePerformanceService.workerPerformance(new Page<>(query.getPageNo(), query.getPageSize()), query));
    }

    @ApiOperation("员工会员业绩")
    @GetMapping("/workerFeesetcost")
    public Result workerFeesetcost(@Validated DashboardQuery query) {
        return Result.ok(storePerformanceService.workerFeesetcost(new Page<>(query.getPageNo(), query.getPageSize()), query));
    }

    @ApiOperation("会员业绩")
    @GetMapping("/member-detail")
    public Result memberPerformance(@Validated DashboardQuery query) {
        return Result.ok(storePerformanceService.salesOfMemberGroup(query));
    }

    @ApiOperation("员工业绩详情")
    @GetMapping("/worker")
    public Result workerPerformance(@Validated DashboardQuery query) {
        // 总销售额
        BigDecimal allSales = storePerformanceService.allSales(query);
        // 会员销售额
        BigDecimal memberSales = storePerformanceService.salesOfMember(query);
        // 总客数
        Integer orderNum = storePerformanceService.num(query, ALL_TYPE);
        // 剪发单数
        Integer numOfCut = storePerformanceService.num(query, CUT_HAIR_TYPE);
        // 剪发金额
        BigDecimal cutHairSales = storePerformanceService.salesOfCutHair(query);
        // 抖音订单
        Integer douyinNum = storePerformanceService.num(query, DOUYIN_TYPE);
        // 会员单数
        Integer memberOrderNum = storePerformanceService.num(query, MEMBER_DETAIL_TYPE);
        // 首次购买单数
        int firstMemberNum = storePerformanceMapper.firstMemberNum(query.getStoreId(), query.getStartDate(), query.getEndDate(), query.getWorkerId());
        // 会员销售总额(按类型分组查询)
        List<Map<String, BigDecimal>> salesOfMemberGroup = storePerformanceService.salesOfMemberGroup(query);
        Integer first = storePerformanceService.num(query.setCustomerType(3), CUT_HAIR_TYPE);
        Integer stranger = storePerformanceService.num(query.setCustomerType(0), CUT_HAIR_TYPE);
        Integer familiar = storePerformanceService.num(query.setCustomerType(1), CUT_HAIR_TYPE);
        Integer deepFamiliar = storePerformanceService.num(query.setCustomerType(2), CUT_HAIR_TYPE);
        //用券理发
        Integer couponCutNum = orderMapper.selectCouponCutNum(query.getStoreId(),query.getWorkerId(),query.getStartDate(),query.getEndDate());
        Map<String, Object> map = new HashMap<>(11);
        map.put("allSales", allSales);
        map.put("memberSales", memberSales);
        map.put("orderNum", orderNum);
        map.put("numOfCut", numOfCut);
        map.put("salesOfCutHair", cutHairSales);
        map.put("douyinNum", douyinNum);
        map.put("memberOrderNum", memberOrderNum);
        map.put("salesOfMemberGroup", salesOfMemberGroup);
        map.put("first", first);
        map.put("stranger", stranger);
        map.put("familiar", familiar);
        map.put("deepFamiliar", deepFamiliar);
        map.put("firstMemberNum", firstMemberNum);
        map.put("couponCutNum", couponCutNum);
        return Result.ok(map);
    }

    @ApiOperation("门店排行")
    @GetMapping("/store-rank")
    public Result storeRank(@Validated DashboardQuery query) {
        return Result.ok(storePerformanceService.storeRank(query, new Page<>(query.getPageNo(), query.getPageSize())));
    }

    @ApiOperation("首页数据")
    @GetMapping("/dashboard-data")
    public Result dashboardData(@Validated DashboardQuery query) {
        BigDecimal allSales = storePerformanceService.allSales(query);
        BigDecimal memberSales = storePerformanceService.salesOfMember(query);
        Integer orderNum = storePerformanceService.num(query, CUT_HAIR_TYPE);
        // 会员订单数
        Integer memberOrderNum = storePerformanceService.num(query, MEMBER_DETAIL_TYPE);

        // 首购会员数
        Integer firstMemberNum = storePerformanceService.num(query.setCustomerType(3), MEMBER_DETAIL_TYPE);
        // 首次消费
        Integer firstConsume = storePerformanceService.num(query.setCustomerType(3), CUT_HAIR_TYPE);
        query.setCustomerType(null);
        // 抖音客数
        Integer douyinNum = storePerformanceService.num(query, DOUYIN_TYPE);
        // 抖音会员转化
        Integer douyinTurnMemberNum = storePerformanceService.douyinTurnMember(query);
        // 新人优惠客数
        Integer newCustomerNum = storePerformanceService.num(query.setType("新人特惠"), CUT_HAIR_TYPE) - douyinNum;
        // 新人会员转化数
        Integer newCusTurnMemNum = storePerformanceService.newCustomerTurnMember(query);

        // 老会员购买
        Integer memberOldNum = storePerformanceService.numIsMeber(query);
        // 新会员购买
        Integer memberNewNum = storePerformanceService.numNotMeber(query);
        // 抖音销售金额
        BigDecimal dySales = storePerformanceService.dySales(query);


        Map<String, Object> map = new HashMap<>(9);
        map.put("allSales", allSales);
        map.put("memberSales", memberSales);
        map.put("orderNum", orderNum);
        map.put("memberOrderNum", memberOrderNum);
        map.put("firstMemberNum", firstMemberNum);
        map.put("firstConsume", firstConsume);
        map.put("douyinNum", douyinNum);
        map.put("douyinTurnMemberNum", douyinTurnMemberNum);
        map.put("newCustomerNum", newCustomerNum);
        map.put("newCusTurnMemNum", newCusTurnMemNum);
        map.put("memberOldNum", memberOldNum);
        map.put("memberNewNum", memberNewNum);
        map.put("dySales", dySales);
        return Result.ok(map);
    }

    @ApiOperation("月份营收")
    @GetMapping("/month")
    public Result performanceOfMonth(@RequestParam @NotNull(message = "月份不能为空") @DateTimeFormat(pattern = "yyyy-MM") Date month,
                                     @RequestParam @NotNull(message = "门店Id不能为空") Integer storeId) {
        return Result.ok(storePerformanceService.getBaseMapper().performanceOfMonth(storeId, month));
    }

    @ApiOperation("门店业绩数据")
    @GetMapping("/data")
    public Result performanceData(@NotNull(message = "门店Id不能为空") Integer storeId) {
        List<Map<String, String>> mapList = storePerformanceService.getBaseMapper().performanceData(storeId);
        mapList.forEach(map -> {
            Map<String, String> map1 = storePerformanceService.getBaseMapper().performanceMeituanAndCika(storeId, map.get("date"));
            map.putAll(map1);
        });
        return Result.ok(mapList);
    }

    @ApiOperation("门店套餐用户数 (有多少买了套餐的用户，并且还有会员券没用完的人)")
    @GetMapping("/storeExistCouponNum")
    public Result storeExistCouponNum() {
        //有多少买了套餐的用户，并且还有会员券没用完的人
        return Result.ok( storePerformanceMapper.storeExistCouponNum());
    }
}
